package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ygqh.baby.ao.CommentType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgMessageMapper;
import com.ygqh.baby.model.YgMessageModel;
import com.ygqh.baby.po.YgMessage;
import com.ygqh.baby.po.YgMessageExample;
import com.ygqh.baby.service.YgMessageService;

@Service
public class YgMessageServiceImpl implements YgMessageService {

	@Autowired
	private YgMessageMapper ygMessageMapper;

	@Override
	public List<YgMessage> find() {
		YgMessageExample example = new YgMessageExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
		return ygMessageMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgMessageExample example = new YgMessageExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
		return ygMessageMapper.countByExample(example);
	}

	@Override
	public YgMessage findById(Long id) {
		return ygMessageMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgMessage ygMessage) {
		return ygMessageMapper.insertSelective(ygMessage);
	}

	@Override
	public int update(YgMessage ygMessage) {
		return ygMessageMapper.updateByPrimaryKeySelective(ygMessage);
	}

	@Override
	public ResultSet<YgMessage> search(QueryInfo queryInfo, String q) {
		List<YgMessage> list = ygMessageMapper.selectSuper(queryInfo, q);
		int count = ygMessageMapper.countSuper(q);
		ResultSet<YgMessage> resultSet = new ResultSet<YgMessage>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids, Long userId) {
		Assert.notEmpty(ids);
		YgMessageExample example = new YgMessageExample();
		example.createCriteria().andIdIn(Arrays.asList(ids)).andUserIdEqualTo(userId);
		YgMessage record = new YgMessage();
		record.setStatus(DataStatus.Delete);

		return ygMessageMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id, Long userId) {
		return this.deleteSoft(new Long[] { id }, userId);
	}

	@Override
	public int saveOrUpdate(YgMessage ygMessage) {
		int r = 0;
		if (ygMessage.getId() != null) {
			r = this.update(ygMessage);
		} else {
			r = this.save(ygMessage);
		}
		return r;
	}

	@Override
	public List<YgMessageModel> findUserMessageList(QueryInfo queryInfo, Long userId, Boolean isRead, CommentType commentType) {
		return ygMessageMapper.selectUserMessageList(queryInfo, userId, isRead, commentType);
	}

	@Override
	public int findUnReadCount(Long userId) {
		return ygMessageMapper.selectUnReadCount(userId);
	}

	@Override
	public int readMessage(Long messageId, Long userId) {
		YgMessageExample example = new YgMessageExample();
		example.createCriteria().andIdEqualTo(messageId).andUserIdEqualTo(userId);
		YgMessage record = new YgMessage();
		record.setIsRead(true);

		return ygMessageMapper.updateByExampleSelective(record, example);
	}

	@Override
	public List<YgMessage> findByReleationId(Long releationId) {
		YgMessageExample example = new YgMessageExample();
		example.createCriteria().andReleationIdEqualTo(releationId);
		return ygMessageMapper.selectByExample(example);
	}

}
